import time

from selenium.webdriver.common.by import By
from common.utils import chromeDriver


class Registration:
    driver = ""
    url = ""

    def __init__(self):
        self.url = "http://8.155.1.153/?s=user/regInfo.html"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    def RegistrationTest(self, username, password, flag):
        #清空用户名，登录密码，验证码
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div:nth-child(1) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div:nth-child(2) > input"))
        chromeDriver.clear(self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div:nth-child(3) > input"))

        #输入用户名，登录密码，验证码
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div:nth-child(1) > input").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div:nth-child(2) > input").sned_keys(password)

        img_text = chromeDriver.getverifycode(self.driver)
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div:nth-child(3) > input").send_keys(img_text)

        #点击同意协议，点击注册
        if flag:
            self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div.agreement.am-margin-top-main > label > span").click()
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-g.user-register-container.theme-data-edit-event > div > div > div.am-radius-lg.am-background-white > div > div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active.am-in > form > div.am-form-group.am-margin-top-main.am-padding-0 > button").click()

        #添加屏幕截图
        chromeDriver.GetScreenShot()

    #注册成功测试
    def RegistreredSuccess(self):
        self.RegistrationTest("li209779", "123456", True)

        time.sleep(3)
        #查找网站logo，确认是否注册成功
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-search.white.am-hide-sm-only > div > div.logo-big.theme-data-edit-event > a > img")

    #注册失败测试
    def RegisterFailed(self):
        #测试用例1，用户名长度<2字符注册
        self.RegistrationTest("a", "123456", True)
        #注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")

        #测试用例2，用户名长度>18个字符注册
        self.RegistrationTest("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "123456", True)
        # 注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")

        #测试用例3，不写用户名注册
        self.RegistrationTest("", "123456", True)
        # 注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")

        #测试用例4，密码长度<6字符注册
        self.RegistrationTest("lilili", "123", True)
        # 注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")

        #测试用例5，密码长度>18字符注册
        self.RegistrationTest("lilili", "123456123456123456123456123456", True)
        # 注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")

        #测试用例6，不写密码注册
        self.RegistrationTest("lilili", "", True)
        # 注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")

        #测试用例7，不点击同意协议
        self.RegistrationTest("lilili", "123456", False)
        # 注册失败后，验证是否还在注册界面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > div.nav-seasrch.header-nav-simple > div > p.login-title.am-fl.am-vertical-align-middle.am-margin-left-main")
